package com.design_mode;

import java.io.*;

/**
 * Created by dustaple on 8/1/14.
 */
public interface PrototypePattern {
    PrototypePattern clone();
}

class Email implements PrototypePattern {
    @Override
    public PrototypePattern clone() {
        return new Email();
    }
}

class Mail implements Cloneable, Serializable {

    @Override
    public Mail clone() {
        Mail pizza = null;
        try {
            pizza = (Mail) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return pizza;
    }

    public Mail deepClone() throws IOException {
        try {
            ObjectOutputStream oos;
            ObjectInputStream ois;

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(this);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);

            return (Mail) ois.readObject();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

class Writer {
    public static void main(String[] args) {


        Mail mail = new Mail();
        Mail mail1 = mail.clone();
        Mail mail2 = null;
        try {
            mail2 = mail.deepClone();
            System.out.println(mail2.getClass().getName() + mail2.getClass());
        } catch (IOException e) {
            e.printStackTrace();
        }

        PrototypePattern email = new Email();
        PrototypePattern email1 = email.clone();

        System.out.println(mail.getClass().getName() + mail.getClass());
        System.out.println(mail1.getClass().getName() + mail1.getClass());
    }
}